#include "gui/customerviewdetailsdialog.h"
#include "gui/separatorwidget.h"
#include "core/constants.h"

/*
  Initialize the dialog's UI. This is NOT related to the method of the
  same name used by the View classes. You don't have to call any default
  implementations from the base class.
  */
void CustomerViewDetailsDialog::initUiComponents()
{
    mainLayout = new QVBoxLayout;
    labeledLayout = new QGridLayout;
    labeledLayout->setColumnStretch(1, 1);

    nameLabel = new QLabel;
    QFont font = nameLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    nameLabel->setFont(font);
    mainLayout->addWidget(nameLabel);
    mainLayout->addWidget(new SeparatorWidget);

    addressMarker = new QLabel("Address:");
    QFont boldFont = addressMarker->font();
    boldFont.setBold(true);
    addressMarker->setFont(boldFont);
    labeledLayout->addWidget(addressMarker, 1, 0);

    addressLabel = new QLabel;
    addressLabel->setWordWrap(true);
    labeledLayout->addWidget(addressLabel, 1, 1);

    contactMarker = new QLabel("Contact number:");
    contactMarker->setFont(boldFont);
    labeledLayout->addWidget(contactMarker, 2, 0);

    contactLabel = new QLabel;
    labeledLayout->addWidget(contactLabel, 2, 1);

    mainLayout->addLayout(labeledLayout);
    mainLayout->addStrut(5);

    noteHeader = new QLabel;
    font = noteHeader->font();
    font.setPointSize(font.pointSize() + 1);
    font.setBold(true);
    noteHeader->setFont(font);
    noteHeader->setText("Notes:");
    mainLayout->addWidget(noteHeader);

    noteLabel = new QLabel;
    noteLabel->setWordWrap(true);
    noteLabel->setTextFormat(Qt::PlainText);
    mainLayout->addWidget(noteLabel);

    mainLayout->addStretch();

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    buttonBox->addButton("&Edit...", QDialogButtonBox::AcceptRole);
    mainLayout->addWidget(buttonBox);

    this->setLayout(mainLayout);
}

void CustomerViewDetailsDialog::initSignalsAndSlots()
{
    // TODO: Add an "edit" button which maps to the accepted() signal
    connect(this->buttonBox, SIGNAL(accepted()),
            this, SLOT(accept()));

    // The "Close" button maps to the rejected() signal
    connect(this->buttonBox, SIGNAL(rejected()),
            this, SLOT(reject()));
}

void CustomerViewDetailsDialog::setCustomer(CustomerModel *customer)
{
    nameLabel->setText(customer->getName());
    addressLabel->setText(customer->getAddress());
    contactLabel->setText(customer->getContactNumber());
    noteLabel->setText(customer->getNote());
}

CustomerViewDetailsDialog::CustomerViewDetailsDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle("Customer Details");
    this->setMinimumSize(320, 240);
    this->initUiComponents();
    this->initSignalsAndSlots();
}
